import 'package:flutter/material.dart';
import '../../utils/http_client.dart';

class GoodsDetailScreen extends StatefulWidget {
  final String taskId;
  final String storeId;

  const GoodsDetailScreen({
    super.key,
    required this.taskId,
    required this.storeId,
  });

  @override
  State<GoodsDetailScreen> createState() => _GoodsDetailScreenState();
}

class _GoodsDetailScreenState extends State<GoodsDetailScreen> {
  bool _isLoading = true;
  List<Map<String, dynamic>> _goods = [];

  @override
  void initState() {
    super.initState();
    _loadGoodsDetail();
  }

  Future<void> _loadGoodsDetail() async {
    try {
      final response = await HttpClient.dio.post(
        '/follower/getGodsDetail',
        data: {
          'taskId': widget.taskId,
          'storeId': widget.storeId,
        },
      );

      if (response.data['success'] == true) {
        setState(() {
          _goods = List<Map<String, dynamic>>.from(response.data['data'] ?? []);
          _isLoading = false;
        });
      } else {
        setState(() {
          _isLoading = false;
        });
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(response.data['message'] ?? '加载失败')),
          );
        }
      }
    } catch (e) {
      setState(() {
        _isLoading = false;
      });
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('网络错误，请稍后重试')),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('货物详情'),
      ),
      body: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : _goods.isEmpty
              ? const Center(child: Text('暂无货物信息'))
              : ListView.builder(
                  padding: const EdgeInsets.all(16),
                  itemCount: _goods.length,
                  itemBuilder: (context, index) {
                    final item = _goods[index];
                    return Card(
                      margin: const EdgeInsets.only(bottom: 16),
                      child: Padding(
                        padding: const EdgeInsets.all(16),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              item['name'] ?? '',
                              style: const TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            const SizedBox(height: 8),
                            _buildInfoRow('数量', '${item['quantity'] ?? 0}'),
                            _buildInfoRow('单位', item['unit'] ?? ''),
                            if (item['remark'] != null)
                              _buildInfoRow('备注', item['remark']),
                          ],
                        ),
                      ),
                    );
                  },
                ),
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        children: [
          SizedBox(
            width: 60,
            child: Text(
              label,
              style: const TextStyle(
                color: Colors.grey,
                fontSize: 14,
              ),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: const TextStyle(fontSize: 14),
            ),
          ),
        ],
      ),
    );
  }
} 